//
//  SignInExplainView.m
//  MobileOffice
//
//  Created by jjkj on 16/12/1.
//  Copyright © 2016年 liaoningjujin. All rights reserved.
//

#import "SignInExplainView.h"
#import "DutyHolidayTableViewCell.h"

@implementation SignInExplainView


- (instancetype)initWithFrame:(CGRect)frame listModel:(DutyDropListModel *)listModel titleArr:(NSArray *)titleArr titleStr:(NSString *)titleStr{
    if (self=[super initWithFrame:frame]) {
        self.backgroundColor=[UIColor clearColor];
        self.listModel=listModel;
        self.dataArr=titleArr;
//        CGFloat viewHeight=listModel.dataArr.count*50;
//        if (viewHeight<250) {
//            viewHeight=250;
//        }else if (viewHeight>IPHONE_HEIGHT-100){
//            viewHeight=IPHONE_HEIGHT-100;
//        }
        
        UIButton *backView=[[UIButton alloc]initWithFrame:frame];
        backView.backgroundColor=[UIColor blackColor];
        backView.alpha=0.4;
        [backView addTarget:self action:@selector(removeViewClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:backView];
        
//        //点击手势
//        UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TwoTapGestureRecognizer:)];
//        tapGesture.delegate=self;
//        [tapGesture setNumberOfTapsRequired:1];
//        [tapGesture setNumberOfTouchesRequired:1];
//        [backView addGestureRecognizer:tapGesture];
        CGFloat tableViewHeight=0;
        //判断如果有内容再给高度
        for (NSString *infoStr in _dataArr) {
            if (infoStr.length>0) {
                tableViewHeight+=64;
            }
        }
        
        //创建列表
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(50, (IPHONE_HEIGHT-(tableViewHeight+70))/2, IPHONE_WIDTH-50*2, tableViewHeight+70) style:UITableViewStylePlain];
        _tableView.layer.masksToBounds=YES;
        _tableView.layer.cornerRadius=8;
        //_myTableView.backgroundColor=[UIColor redColor];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.tag=110;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator=NO;
        _tableView.showsHorizontalScrollIndicator=NO;
        _tableView.bounces=NO;
        //[self addTableViewUpdateRefresh:_tableView BindingIdentity:@""];
        //    [self addTableViewLoadMoreData:_tableView BindingIdentity:@""];
        [self addSubview:_tableView];
        
        //头视图
        UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, _tableView.width, 70)];
        headView.backgroundColor=[UIColor whiteColor];
        _tableView.tableHeaderView=headView;
        
        
        UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(8, 10, _tableView.width-16, headView.height-20)];
        titleLabel.numberOfLines=0;
        titleLabel.font=[UIFont boldSystemFontOfSize:20];
        titleLabel.textColor=COLOR_MIDBLUE;
        titleLabel.textAlignment=NSTextAlignmentCenter;
        if ([titleStr isEqualToString:@"签到说明"]||[titleStr isEqualToString:@"签退说明"]) {
            titleLabel.text=titleStr;
        }else{
            //设置富文本
            NSString *str2 = [NSString stringWithFormat:@"您%@了，需要说明吗?",titleStr];
            NSMutableAttributedString *str3 = [[NSMutableAttributedString alloc] initWithString: str2]; //创建一个NSMutableAttributedString
            [str3 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(1,  titleStr.length)]; // 关键步骤，设置指定位置文字的颜色
            [str3 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20.0f] range:NSMakeRange(1,titleStr.length)]; //关键步骤，设置指定位置文字的字号大小
            titleLabel.attributedText=str3;
        }
        [headView addSubview:titleLabel];
        
        UILabel *lineLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, headView.height-1, _tableView.width, 1)];
        lineLabel.backgroundColor=BACKCOLOR_GRAY;
        [headView addSubview:lineLabel];
        
        //        CATransition *animation = [CATransition animation];
        //
        //        animation.duration = 0.2f;
        //
        //        animation.timingFunction = UIViewAnimationCurveEaseInOut;
        //
        //        animation.fillMode = kCAFillModeForwards;
        //
        //        //各种动画效果
        //
        //        /*
        //
        //         KCATransitionFade;
        //
        //         KCATransitionMoveIn;
        //
        //         KCATransitionPush;
        //
        //         KCATransitionReveal;
        //
        //         */
        //
        //        /*
        //
        //         KCATransitionFromeRight;
        //
        //         KCATransitionFromLeft;
        //
        //         KCATransitionFormTop;
        //
        //         kCATransitionFromButtons;
        //
        //         */
        //
        //        //各种组合
        //
        //        animation.type = kCATransitionFade;
        //
        //        animation.subtype = kCATransitionFromRight;
        //
        //
        //        [self.layer addAnimation:animation forKey:@"animation"];
        
        
    }
    return self;
}

#pragma mark - TableViewDelagate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==1) {
        NSString *cellID=@"DutyHolidayTableViewCell";
        DutyHolidayTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell=[[DutyHolidayTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID row:indexPath.row+1 model:self.listModel viewWidth:_tableView.width];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.myTitle.x=8;
            cell.myTitle.width=_tableView.width-16;
            cell.myTitle.font=FONT_17;
            cell.myTitle.textAlignment=NSTextAlignmentCenter;
            cell.myTitle.textColor=COLOR_MIDBLUE;
            cell.myBlock=^(NSInteger selectRow,NSInteger selectDuty,NSString *dutyTypeStr,NSString *businessCode){
//                //根据保存的开关状态决定是否显示提示框
//                    __weak ParentsViewController *mySelf=self.homeController;
                    __weak SignInExplainView *myView=self;
//                    PromptTimeView *promptTimeView=[[PromptTimeView alloc]initWithFrame:mySelf.view.bounds message:@"请假必须在40分钟内完成,确认进入申请页面吗?" prompt:@"知道了,下次不再提醒"];
//                    //确定回调函数
//                    promptTimeView.promptTimeBlock=^(){
//                        [myView postViewControllerWithSelectDuty:selectDuty dutyTypeStr:dutyTypeStr businessCode:businessCode];
//                    };
//                    //取消回调函数
//                    promptTimeView.promptTimeCancelBlock=^(){
//                        //_promptTimeView=nil;
//                    };
//                    [mySelf.view addSubview:promptTimeView];
                    [myView postViewControllerWithSelectDuty:selectDuty dutyTypeStr:dutyTypeStr businessCode:businessCode];
                [myView removeViewClick];
            };
        }
        if (!_cellOpen) {
            cell.lineLabel.y=63;
            cell.footView.hidden=YES;
        }else{
            cell.lineLabel.y=63+self.listModel.dataArr.count/3*(40+15)+15;
            cell.footView.hidden=NO;
        }
        cell.myTitle.text=_dataArr[indexPath.row];
        return cell;
    }
    //公出 异常
    NSString *cellID=@"DutyListTableViewCell";
    DutyListTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell=[[DutyListTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
        cell.selectionStyle=UITableViewCellSelectionStyleDefault;
        cell.myTitle.x=8;
        cell.myTitle.width=_tableView.width-16;
        cell.myTitle.textAlignment=NSTextAlignmentCenter;
        cell.myTitle.textColor=COLOR_MIDBLUE;
        cell.myTitle.font=FONT_17;
    }
    //只有最后一行为灰色
    if (indexPath.row==_dataArr.count-1) {
       cell.myTitle.textColor=[UIColor grayColor];
    }else{
        cell.myTitle.textColor=COLOR_MIDBLUE;
    }
    cell.myTitle.text=_dataArr[indexPath.row];
    return cell;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (_myBlock) {
//        _myBlock(_dataArr[indexPath.row]);
//        [self removeFromSuperview];
//    }
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([_dataArr[indexPath.row] length]==0) return 0;
    if (indexPath.row==1) {
        if (!_cellOpen) {
            return 64;
        }else{
            return self.listModel.dataArr.count/3*(40+15)+15+64;
        }
    }
    return 64;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //如果是第一第二个cell
    if (indexPath.row==1){
        _cellOpen=!_cellOpen;
        [UIView animateWithDuration:0.2 animations:^{
            //设置tableView的frame
            CGFloat tableViewHeight=0;
            //判断如果有内容再给高度
            for (NSString *infoStr in _dataArr) {
                if (infoStr.length>0) {
                    tableViewHeight+=64;
                }
            }
            //调整tableView的高度
            if (!_cellOpen) {
                _tableView.height=tableViewHeight+70;
                _tableView.y=(IPHONE_HEIGHT-_tableView.height)/2;
            }else{
                _tableView.height=tableViewHeight+70+self.listModel.dataArr.count/3*(40+15)+15;
                _tableView.y=(IPHONE_HEIGHT-_tableView.height)/2;
            }
        }];
        [_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
    }else if (indexPath.row==0){
        SignInAbnormalViewController *overTime=[SignInAbnormalViewController new];
        overTime.navaTitle=@"异常考勤说明";
        //        overTime.businessCode=[businessCode copy];
        //        overTime.dutyType=selectRow;
        //        overTime.keywordId=[model.keywordId copy];
        [self.homeController.navigationController pushViewController:overTime animated:YES];
        [self removeFromSuperview];
    }else if(indexPath.row==2){
        GoOutApplyViewController *goout=[GoOutApplyViewController new];
        //        DutyGoBackViewController *goout=[DutyGoBackViewController new];
        goout.navaTitle=@"公出";
        [self.homeController.navigationController pushViewController:goout animated:YES];
        [self removeFromSuperview];
    }else{
        [self removeFromSuperview];
    }
}

- (void)postViewControllerWithSelectDuty:(NSInteger)selectDuty dutyTypeStr:(NSString *)dutyTypeStr businessCode:(NSString *)businessCode{
    __weak ParentsViewController *mySelf=self.homeController;
    DutyDropListModel *model=_listModel.dataArr[selectDuty];
    switch (selectDuty) {
        case 0:
        {
            OffDetailViewController *off=[OffDetailViewController new];
            off.navaTitle=[dutyTypeStr copy];
            off.businessCode=[businessCode copy];
            off.dutyType=0;
            off.keywordId=[model.keywordId copy];
            [mySelf.navigationController pushViewController:off animated:YES];
        }
            break;
        case 1:
        {
            FamilyLeaveDetailViewController *off=[FamilyLeaveDetailViewController new];
            off.navaTitle=[dutyTypeStr copy];
            off.businessCode=[businessCode copy];
            off.dutyType=0;
            off.keywordId=[model.keywordId copy];
            [mySelf.navigationController pushViewController:off animated:YES];
        }
            break;
        case 2:
        {
            InjuryDetailViewController *off=[InjuryDetailViewController new];
            off.navaTitle=[dutyTypeStr copy];
            off.businessCode=[businessCode copy];
            off.dutyType=0;
            off.keywordId=[model.keywordId copy];
            [mySelf.navigationController pushViewController:off animated:YES];
        }
            break;
        case 3:
        {
            YearDetailViewController *off=[YearDetailViewController new];
            off.navaTitle=[dutyTypeStr copy];
            off.businessCode=[businessCode copy];
            off.dutyType=0;
            off.keywordId=[model.keywordId copy];
            [mySelf.navigationController pushViewController:off animated:YES];
        }
            break;
        case 4:
        {
            MaternityDetailViewController *off=[MaternityDetailViewController new];
            off.navaTitle=[dutyTypeStr copy];
            off.businessCode=[businessCode copy];
            off.dutyType=0;
            off.keywordId=[model.keywordId copy];
            [mySelf.navigationController pushViewController:off animated:YES];
        }
            break;
        case 5:
        {
            FuneralDetailViewController *off=[FuneralDetailViewController new];
            off.navaTitle=[dutyTypeStr copy];
            off.businessCode=[businessCode copy];
            off.dutyType=0;
            off.keywordId=[model.keywordId copy];
            [mySelf.navigationController pushViewController:off animated:YES];
        }
            break;
        case 6:
        {
            //            MarriageDetailViewController *off=[MarriageDetailViewController new];
            //            off.navaTitle=[dutyTypeStr copy];
            //            off.businessCode=[businessCode copy];
            //            off.dutyType=selectRow;
            //            off.keywordId=[model.keywordId copy];
            //            [mySelf.navigationController pushViewController:off animated:YES];
            FuneralDetailViewController *off=[FuneralDetailViewController new];
            off.navaTitle=[dutyTypeStr copy];
            off.businessCode=[businessCode copy];
            off.dutyType=0;
            off.keywordId=[model.keywordId copy];
            [mySelf.navigationController pushViewController:off animated:YES];
        }
            break;
        case 7:
        {
            PersonalDetailViewController *off=[PersonalDetailViewController new];
            off.navaTitle=[dutyTypeStr copy];
            off.businessCode=[businessCode copy];
            off.dutyType=0;
            off.keywordId=[model.keywordId copy];
            [mySelf.navigationController pushViewController:off animated:YES];
        }
            break;
        case 8:
        {
            SickDetailViewController *off=[SickDetailViewController new];
            off.navaTitle=[dutyTypeStr copy];
            off.businessCode=[businessCode copy];
            off.dutyType=0;
            off.keywordId=[model.keywordId copy];
            [mySelf.navigationController pushViewController:off animated:YES];
        }
            break;
        default:
            break;
    }
    
}

- (void)removeViewClick{
    [self removeFromSuperview];
}

//#pragma mark -手势点击时间
//-(void)TwoTapGestureRecognizer:(UIGestureRecognizer*)gr{
//    [self endEditing:YES];
//    [self removeFromSuperview];
//}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
